package com.soushin.mvpdemo;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.ALog;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.soushin.mvpdemo.Utils.ActivityUtil;
import com.soushin.mvpdemo.module.bigimage.BigImageActivity;
import com.soushin.mvpdemo.module.login.LoginActivity;
import com.soushin.mvpdemo.module.main.MainActivity;
import com.soushin.mvpdemo.module.rxjava.RxjavaActivity;
import com.soushin.mvpdemo.module.rxjava.example.ExampleActivity;
import com.soushin.mvpdemo.module.rxjava.operator.OperatorActivity;
import com.soushin.mvpdemo.network.HttpClient;

/**
 * activity生命周期管理类
 * 你想象力有多丰富,这里就有多强大,
 * 以前放到BaseActivity的操作都可以放到这里
 * 使用:registerActivityLifecycleCallbacks(new ActivityLifeCycleCallBackIml());
 *
 * @author SouShin
 * @time 2018/12/10 15:38
 */
public class ActivityLifeCycleCallBackIml implements Application.ActivityLifecycleCallbacks {
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        ALog.i("activity生命周期管理类", "onActivityCreated");
        ActivityUtil.addActivity(activity);
    }

    @Override
    public void onActivityStarted(final Activity activity) {
        ALog.i("activity生命周期管理类", "onActivityStarted");
        //这里根据不同的activity显示不同的topBar
        // TODO: 2018/12/10 可以考虑使用ActionBar设置自定义布局
        if (activity instanceof MainActivity) {
            setActionBar(activity, false, activity.getString(R.string.app_name));
        } else if ((activity instanceof BigImageActivity)) {
            setActionBar(activity, true, "查看大图相关");
        } else if ((activity instanceof RxjavaActivity)) {
            setActionBar(activity, true, "Rxjava2相关");
        }else if (activity instanceof OperatorActivity||activity instanceof ExampleActivity) {
            setActionBar(activity, true, activity.getIntent().getStringExtra(RxjavaActivity.key_title));
        }else if (activity instanceof LoginActivity) {
            setActionBar(activity, true,"登录");
        }
    }

    /**
     * @param activity
     * @param isBack  需/不需要返回按钮
     * @param title
     */
    private void setActionBar(final Activity activity, boolean isBack, String title) {
        View toolBar = activity.findViewById(R.id.toolbar);
        ToolHolder toolHolder = new ToolHolder(toolBar);
        ImmersionBar.with(activity)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(toolBar)
                .init();
        toolHolder.tvTitle.setText(title);
        toolHolder.imgBack.setVisibility(isBack ? View.VISIBLE : View.GONE);
        toolHolder.imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
    }

    @Override
    public void onActivityResumed(Activity activity) {
        ALog.i("activity生命周期管理类", "onActivityResumed");
    }

    @Override
    public void onActivityPaused(Activity activity) {
        ALog.i("activity生命周期管理类", "onActivityPaused");
    }

    @Override
    public void onActivityStopped(Activity activity) {
        ALog.i("activity生命周期管理类", "onActivityStopped");
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        ALog.i("activity生命周期管理类", "onActivitySaveInstanceState");

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        ALog.i("activity生命周期管理类", "onActivityDestroyed");
        ActivityUtil.removeActivity(activity);
        ImmersionBar.with(activity).destroy(); //必须调用该方法，防止内存泄漏
        HttpClient.disposeRequest();
    }

    static class ToolHolder {
        @BindView(R.id.img_back)
        ImageView imgBack;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.toolbar)
        ConstraintLayout toolbar;

        ToolHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
